<template>
  <div class="tree-container">
    <el-tree
      :data="treeData"
      node-key="id"
      default-expand-all
      :expand-on-click-node="true"
    >
      <div slot-scope="{ node, data }" class="custom-tree-node">
        <span>
          {{ data.code }} - {{ data.name }} x{{ data.quantity }}
        </span>
        <span>
          <el-button
            size="mini"
            type="text"
            @click.stop="append(node, data)"
          >
            新增子节点
          </el-button>
          <el-button
            v-if="!data.isParent"
            size="mini"
            type="text"
            style="color:#F56C6C"
            @click.stop="remove(node, data)"
          >
            删除
          </el-button>
        </span>
      </div>
    </el-tree>

    <el-dialog title="新增节点" :visible.sync="dialogVisible" width="30%">
      <el-form 
        :model="newNodeForm" 
        :rules="rules"
        ref="newNodeForm"
        label-width="80px"
      >
        <el-form-item label="编码" prop="code">
          <el-input v-model="newNodeForm.code" placeholder="如V/L004"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="newNodeForm.name" placeholder="如螺丝"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input-number v-model="newNodeForm.quantity" :min="1"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      treeData: [
        {
          id: '001',
          code: '001',
          name: '主机',
          quantity: 1,
          isParent:true,
          children: [
            {
              id: '002',
              code: '002',
              name: '电机',
              quantity: 1,
              children: [
                { id: 'L004-1', code: 'V/L004', name: '螺丝', quantity: 4 },
                { id: 'L006', code: 'V/L006', name: '传感器', quantity: 2 }
              ]
            },
            {
              id: 'L003',
              code: 'V/L003',
              name: '控制板',
              quantity: 1,
              children: [
                { id: 'L004-2', code: 'V/L004', name: '螺丝', quantity: 2 },
                { id: 'L009-1', code: 'V/L009', name: '电源插头', quantity: 1 }
              ]
            },
            {
              id: 'L005',
              code: 'V/L005',
              name: '外壳',
              quantity: 1,
              children: []
            },
            {
              id: 'L007',
              code: 'V/L007',
              name: '风扇',
              quantity: 2,
              children: []
            },
            {
              id: 'L008',
              code: 'V/L008',
              name: '电源板',
              quantity: 1,
              children: [
                { id: 'L009-2', code: 'V/L009', name: '电源插头', quantity: 1 },
                { id: 'L004-3', code: 'V/L004', name: '螺丝', quantity: 2 }
              ]
            },
            {
              id: 'L010',
              code: 'V/L010',
              name: '显示屏',
              quantity: 1,
              children: [
                { id: 'L004-4', code: 'V/L004', name: '螺丝', quantity: 2 }
              ]
            }
          ]
        }
      ],
      dialogVisible: false,
      newNodeForm: {
        code: '',
        name: '',
        quantity: 1
      },
      rules: {
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: '请填写数量', trigger: 'blur' }
        ]
      },
      currentNode: null
    };
  },
  methods: {
    append(node, data) {
      this.currentNode = { node, data };
      this.newNodeForm = {
        code: '',
        name: '',
        quantity: 1
      };
      this.dialogVisible = true;
      // 清除之前的验证结果
      this.$nextTick(() => {
        if (this.$refs.newNodeForm) {
          this.$refs.newNodeForm.clearValidate();
        }
      });
    },
    remove(node, data) {
      this.$confirm('确定删除该节点吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {

});
    },
    submitForm() {
      this.$refs.newNodeForm.validate((valid) => {
        if (valid) {
          this.confirmAddNode();
        } else {
          return false;
        }
      });
    },
    confirmAddNode() {
      const newNode = {
        id: `${this.newNodeForm.code}-${Date.now()}`,
        code: this.newNodeForm.code,
        name: this.newNodeForm.name,
        quantity: this.newNodeForm.quantity,
        children: []
      };
      
      if (!this.currentNode.data.children) {
        this.$set(this.currentNode.data, 'children', []);
      }
      this.currentNode.data.children.push(newNode);
      this.dialogVisible = false;
      this.$message.success('新增成功');
    }
  }
};
</script>

<style scoped lang='less'>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.tree-container {
  width: 400px;
  margin: 20px auto;
  ::v-deep .el-tree-node__expand-icon{
    color:#000000
  } 
  ::v-deep .el-tree-node__expand-icon.is-leaf {
    color: transparent;
    cursor: default;
}
}
</style>